{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 3. 处理重复值",
   "id": "b91524164ea64aa"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T06:59:01.631155Z",
     "start_time": "2025-09-12T06:59:01.154394Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day15/file/'"
   ],
   "id": "57bc244164a5d201",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "## 3.1 找出重复数据",
   "id": "dedeb4666ced9387"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:04:18.074577Z",
     "start_time": "2025-09-12T07:04:18.056317Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '成绩3.xlsx',index_col='序号')\n",
    "print(df)\n",
    "# 获取唯一值\n",
    "print(f'唯一姓名:{df['姓名'].unique()}')\n",
    "\n",
    "# 统计每个姓名出现的次数\n",
    "print(f'每个姓名出现的次数:{df['姓名'].value_counts()}')\n",
    "\n",
    "# 判断整行是否重复\n",
    "print(f'数据是否重复:{df['姓名'].duplicated()}')\n",
    "\n",
    "\n",
    "# 判断特定列是否重复\n",
    "print(f'姓名是否重复：{df.duplicated(subset=[\"姓名\"])}')\n"
   ],
   "id": "539b50cf51ce3250",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     姓名 性别   成绩\n",
      "序号             \n",
      "1   聂茹凤  女   70\n",
      "2   聂茹凤  女  100\n",
      "3   谭鑫宇  女   75\n",
      "4   韩耀祖  男   65\n",
      "5   刘千琪  女   75\n",
      "6   刘千琪  女  100\n",
      "7   崔龙腾  男   78\n",
      "8   李欣桐  女   96\n",
      "9   李兆康  男   85\n",
      "10  李伟聪  男   32\n",
      "11  韩耀祖  男  100\n",
      "12  李欣桐  女  100\n",
      "13  李兆康  男  100\n",
      "唯一姓名:['聂茹凤' '谭鑫宇' '韩耀祖' '刘千琪' '崔龙腾' '李欣桐' '李兆康' '李伟聪']\n",
      "每个姓名出现的次数:姓名\n",
      "聂茹凤    2\n",
      "韩耀祖    2\n",
      "刘千琪    2\n",
      "李欣桐    2\n",
      "李兆康    2\n",
      "谭鑫宇    1\n",
      "崔龙腾    1\n",
      "李伟聪    1\n",
      "Name: count, dtype: int64\n",
      "数据是否重复:序号\n",
      "1     False\n",
      "2      True\n",
      "3     False\n",
      "4     False\n",
      "5     False\n",
      "6      True\n",
      "7     False\n",
      "8     False\n",
      "9     False\n",
      "10    False\n",
      "11     True\n",
      "12     True\n",
      "13     True\n",
      "Name: 姓名, dtype: bool\n",
      "姓名是否重复：序号\n",
      "1     False\n",
      "2      True\n",
      "3     False\n",
      "4     False\n",
      "5     False\n",
      "6      True\n",
      "7     False\n",
      "8     False\n",
      "9     False\n",
      "10    False\n",
      "11     True\n",
      "12     True\n",
      "13     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3.2 删除重复数据",
   "id": "b98240d16117248d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:10:34.173625Z",
     "start_time": "2025-09-12T07:10:34.156956Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '成绩3.xlsx',index_col='序号')\n",
    "print(df)\n",
    "\n",
    "# 删除所有列中完全重复的行\n",
    "df.drop_duplicates(inplace=True)\n",
    "\n",
    "# 根据姓名删除重复的行，只保留出现的第一个\n",
    "# df.drop_duplicates(subset=['姓名'], inplace=True,keep='first')\n",
    "\n",
    "# 根据姓名删除重复的行，只保留出现的最后一个\n",
    "# df.drop_duplicates(subset=['姓名'], inplace=True,keep='last')\n",
    "\n",
    "# 删除所有重复的项\n",
    "df.drop_duplicates(subset=['姓名'], inplace=True,keep=False)\n",
    "\n",
    "print(df)"
   ],
   "id": "3d603bb856943114",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     姓名 性别   成绩\n",
      "序号             \n",
      "1   聂茹凤  女   70\n",
      "2   聂茹凤  女  100\n",
      "3   谭鑫宇  女   75\n",
      "4   韩耀祖  男   65\n",
      "5   刘千琪  女   75\n",
      "6   刘千琪  女  100\n",
      "7   崔龙腾  男   78\n",
      "8   李欣桐  女   96\n",
      "9   李兆康  男   85\n",
      "10  李伟聪  男   32\n",
      "11  韩耀祖  男  100\n",
      "12  李欣桐  女  100\n",
      "13  李兆康  男  100\n",
      "14  李兆康  男  100\n",
      "14  李兆康  男  100\n",
      "     姓名 性别  成绩\n",
      "序号            \n",
      "3   谭鑫宇  女  75\n",
      "7   崔龙腾  男  78\n",
      "10  李伟聪  男  32\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3.3 提取重复",
   "id": "f334d140040676ee"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:15:55.503157Z",
     "start_time": "2025-09-12T07:15:55.352833Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.read_excel(path + '成绩3.xlsx',index_col='序号')\n",
    "data = df.copy()\n",
    "print(data)\n",
    "\n",
    "# 提取所有重复的行\n",
    "# print(data.duplicated(keep=False))\n",
    "\n",
    "# 提取名字重复的行\n",
    "# print(data.duplicated(subset=['姓名'], keep=False))\n",
    "\n",
    "# # 提取第二次及以后出现的重复行\n",
    "# print(data.duplicated(subset=['姓名'], keep='first'))\n",
    "\n",
    "# 提取第一次出现的重复行\n",
    "print(data.duplicated(subset=['姓名'], keep='last'))"
   ],
   "id": "57d59e154c7ee323",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     姓名 性别   成绩\n",
      "序号             \n",
      "1   聂茹凤  女   70\n",
      "2   聂茹凤  女  100\n",
      "3   谭鑫宇  女   75\n",
      "4   韩耀祖  男   65\n",
      "5   刘千琪  女   75\n",
      "6   刘千琪  女  100\n",
      "7   崔龙腾  男   78\n",
      "8   李欣桐  女   96\n",
      "9   李兆康  男   85\n",
      "10  李伟聪  男   32\n",
      "11  韩耀祖  男  100\n",
      "12  李欣桐  女  100\n",
      "13  李兆康  男  100\n",
      "14  李兆康  男  100\n",
      "14  李兆康  男  100\n",
      "序号\n",
      "1      True\n",
      "2     False\n",
      "3     False\n",
      "4      True\n",
      "5      True\n",
      "6     False\n",
      "7     False\n",
      "8      True\n",
      "9      True\n",
      "10    False\n",
      "11    False\n",
      "12    False\n",
      "13     True\n",
      "14     True\n",
      "14    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3.4 多条件去重\n",
   "id": "b59bec67f8ec29f6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-12T07:18:23.164376Z",
     "start_time": "2025-09-12T07:18:23.148089Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#根据姓名和性别组合去重\n",
    "df = pd.read_excel(path + '成绩3.xlsx',index_col='序号')\n",
    "data = df.copy()\n",
    "data.drop_duplicates(subset=['姓名','性别'], inplace=True,keep=False)\n",
    "print(data)"
   ],
   "id": "468eb7523ef144cf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     姓名 性别   成绩\n",
      "序号             \n",
      "1   聂茹凤  女   70\n",
      "2   聂茹凤  男  100\n",
      "3   谭鑫宇  女   75\n",
      "7   崔龙腾  男   78\n",
      "10  李伟聪  男   32\n"
     ]
    }
   ],
   "execution_count": 30
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
